package string;

import java.util.Arrays;

/**
 * 重载的拆分方法
 * String[] split(String regex,int limit)
 * 拆分字符串，返回字符串数组
 * limit用于指定拆分出多少项
 */
public class SplitDemo2 {
    public static void main(String[] args) {
        String line = "a=b=c=d=e=d=f========";
        String[] arr = line.split("=");
        System.out.println(arr.length);
        System.out.println(Arrays.toString(arr));

        String[] arr2 = line.split("=", 2);
        System.out.println(arr2.length);
        System.out.println(Arrays.toString(arr2));

        arr2 = line.split("=", 3);
        System.out.println(arr2.length);
        System.out.println(Arrays.toString(arr2));
        /*
            如果limit>最多可拆分项时，则返回实际最多可拆分项，包含最后连续
            拆分出来的空字符串(原本被舍弃的)
         */
        arr2 = line.split("=", 30);
        System.out.println(arr2.length);
        System.out.println(Arrays.toString(arr2));
        /*
            如果limit为0,则与默认拆分相同(忽略最后连续拆分出的空字符串)
         */
        arr2 = line.split("=", 0);
        System.out.println(arr2.length);
        System.out.println(Arrays.toString(arr2));
        /*
            当limit<0时，应拆尽拆，保留所有可拆分项(包括最后连续拆分出的空字符串)
         */
        arr2 = line.split("=", -1);
        System.out.println(arr2.length);
        System.out.println(Arrays.toString(arr2));
    }
}


